//
//  WriteFormViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 28..
//  Copyright 2011 Daum. All rights reserved.
//

#import "WriteFormViewController.h"
static NSString *kCellTextField_ID = @"CellTextField_ID";
static NSString *kSectionTitleKey = @"sectionTitleKey";
static NSString *kSourceKey = @"sourceKey";
const NSInteger kViewTag = 1;

@interface WriteFormViewController(Private)
- (void) initFormSubViews;
- (void) loadDefaultData;
@end

@implementation WriteFormViewController

@synthesize writeDic = _writeDic;
@synthesize dataSourceArray = _dataSourceArray;


@synthesize titleTextField = _titleTextField;

@synthesize itemBtn = _itemBtn;

@synthesize accountTextField = _accountTextField;
@synthesize accountSegment = _accountSegment;
@synthesize accountSegment2 = _accountSegment2;

@synthesize shopTextField = _shopTextField;
@synthesize shopSearchButton = _shopSearchButton;

@synthesize payLabel = _payLabel;
@synthesize payTypeSegment = _payTypeSegment;

@synthesize writeDayButton = _writeDayButton;

@synthesize repeatSegment = _repeatSegment;

@synthesize tagTextField = _tagTextField;
@synthesize tagSearchButton = _tagSearchButton;

@synthesize itemPickerViewController = _itemPickerViewController;
@synthesize shopPickerViewController = _shopPickerViewController;
@synthesize dayPickerViewController = _dayPickerViewController;
@synthesize payPickerViewController = _payPickerViewController;
@synthesize tagPickerViewController = _tagPickerViewController;
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        NSLog(@"initWithNibName");
        self.writeDic = [NSMutableDictionary dictionary];
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    NSLog(@"dealloc");
    [_writeDic release];
    [_dataSourceArray release];
    
    

    [_titleTextField release];
    
    [_itemBtn release];
    
    [_accountTextField release];
    [_accountSegment release];
    [_accountSegment2 release];
    
    [_shopTextField release];
    [_shopSearchButton release];

    [_payLabel release];
    [_payTypeSegment release];
    
    [_writeDayButton release];
    
    [_repeatSegment release];
    
    [_tagTextField release];
    [_tagSearchButton release];
    
    [_itemPickerViewController release];
    [_shopPickerViewController release];
    [_dayPickerViewController release];
    [_payPickerViewController release];
    [_tagPickerViewController release];
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    NSLog(@"didReceiveMemoryWarning");
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];

    
	self.dataSourceArray = [NSArray arrayWithObjects:
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"내용", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_TITLE], kSourceKey,                             
							 nil],
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"분류", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_ITEM], kSourceKey,
							 nil],							
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"가격", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_ACCOUNT], kSourceKey,
							 nil],	
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"거래처", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_SHOP], kSourceKey,
							 nil], 
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"결제타입", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_PAY], kSourceKey,
							 nil],
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"등록일", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_WRITE_DAY], kSourceKey,
							 nil],     
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"반복설정", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_REPEAT], kSourceKey,
							 nil],   
                            [NSDictionary dictionaryWithObjectsAndKeys:
                             @"태그", kSectionTitleKey,
                             [NSNumber numberWithInt:CELL_TAG], kSourceKey,
							 nil],                            
							nil];    
    self.title = @"수입/지출 등록";
    UIBarButtonItem *submitBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                                            target:self
                                                                            action:@selector(submitAction)];
    [self.navigationItem setRightBarButtonItem:submitBtn];
    [self loadDefaultData];
    [self initFormSubViews];
    
    self.itemPickerViewController = [[[ItemPickerViewController alloc] initWithNibName:@"ItemPickerView" bundle:nil] autorelease];
    [_itemPickerViewController setPickerResultDelegate:self];
 
    self.shopPickerViewController = [[[ShopPickerViewController alloc] initWithNibName:@"ShopPickerView" bundle:nil] autorelease];
    [_shopPickerViewController setPickerResultDelegate:self];    

    self.dayPickerViewController = [[[DayPickerViewController alloc] initWithNibName:@"DayPickerView" bundle:nil] autorelease];
    [_dayPickerViewController setPickerResultDelegate:self];        

    self.payPickerViewController = [[[PayPickerViewController alloc] initWithNibName:@"PayPickerView" bundle:nil] autorelease];
    [_payPickerViewController setPickerResultDelegate:self];

    self.tagPickerViewController = [[[TagPickerViewController alloc] initWithNibName:@"TagPickerView" bundle:nil] autorelease];
    [_tagPickerViewController setPickerResultDelegate:self];        

}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    NSLog(@"viewDidUnload");
    self.writeDic = nil;
    self.dataSourceArray = nil;
        
    self.titleTextField =  nil;
    
    self.itemBtn =  nil;

    self.accountTextField =  nil;
    self.accountSegment = nil;
    self.accountSegment2 = nil;
    
    self.shopTextField =  nil;
    self.shopSearchButton =  nil;
    
    self.payLabel = nil;
    self.payTypeSegment =  nil;
    
    self.writeDayButton =  nil;
    
    self.repeatSegment =  nil;
    
    self.tagTextField =  nil;
    self.tagSearchButton =  nil;
    
    self.itemPickerViewController = nil;
    self.shopPickerViewController = nil;
    self.dayPickerViewController = nil;
    self.payPickerViewController = nil;
    self.tagPickerViewController = nil;
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"viewWillAppear");
    [super viewWillAppear:animated];
    [_writeDic setObject:[NSDate date] forKey:@"writeDay"];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    NSLog(@"viewWillDisappear");
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    NSLog(@"shouldAutorotateToInterfaceOrientation");
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return NO;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"willRotateToInterfaceOrientation");
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}


#pragma mark -
#pragma mark User methods
/////////////////////////////////////////////////////////////////////////////////////////////////
- (UITextField*) createTextFieldWithFrame:(CGRect)frame{
    UITextField* textField = [[UITextField alloc] initWithFrame:frame];
    
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textColor = [UIColor blackColor];
    textField.font = [UIFont systemFontOfSize:15.0];
    textField.backgroundColor = [UIColor whiteColor];
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.delegate = self;	
    
    return textField;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) initFormSubViews{
    CGRect basicFrame = CGRectMake(kTextFieldLeftMargin, 8.0, kTextFieldWidth, kTextFieldHeight);
    CGRect secondFrame = CGRectMake(kTextFieldLeftMargin, 43.0, kTextFieldWidth, kTextFieldHeight);
    CGRect thirdFrame = CGRectMake(kTextFieldLeftMargin, 78.0, kTextFieldWidth, kTextFieldHeight);

    self.titleTextField = [self createTextFieldWithFrame:basicFrame];
    self.titleTextField.placeholder = @"내용을 입력 해 주세요.";
    self.titleTextField.tag = kViewTag;
    
    self.itemBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.itemBtn.frame = basicFrame;
    [self.itemBtn setTitle:@"분류를 선택해 주세요." forState:UIControlStateNormal];
    [self.itemBtn addTarget:self action:@selector(itemBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.itemBtn.tag = kViewTag;
    
    self.accountTextField = [self createTextFieldWithFrame:basicFrame];
//    self.accountTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    self.accountTextField.tag = kViewTag;
    [self.accountTextField addTarget:self action:@selector(accountTextEditingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
    [self.accountTextField setAccessibilityLabel:NSLocalizedString(@"SecureTextField", @"")];
    
    self.accountSegment = [[UISegmentedControl alloc] initWithFrame:secondFrame];
    [self.accountSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [self.accountSegment setMomentary:YES];
    [self.accountSegment insertSegmentWithTitle: @"천만" atIndex: 0 animated: NO ];
    [self.accountSegment insertSegmentWithTitle: @"백만" atIndex: 1 animated: NO ];
    [self.accountSegment insertSegmentWithTitle: @"십만" atIndex: 2 animated: NO ];
    [self.accountSegment insertSegmentWithTitle: @"만" atIndex: 3 animated: NO ];
    [self.accountSegment addTarget:self action:@selector(accountSegmentAction:) forControlEvents:UIControlEventValueChanged];
    self.accountSegment.tag = kViewTag;
    
    self.accountSegment2 = [[UISegmentedControl alloc] initWithFrame:thirdFrame];
    [self.accountSegment2 setSegmentedControlStyle:UISegmentedControlStyleBar];
    [self.accountSegment2 setMomentary:YES];    
    [self.accountSegment2 insertSegmentWithTitle: @"천" atIndex: 0 animated: NO ];
    [self.accountSegment2 insertSegmentWithTitle: @"백" atIndex: 1 animated: NO ];
    [self.accountSegment2 insertSegmentWithTitle: @"십" atIndex: 2 animated: NO ];    
    [self.accountSegment2 insertSegmentWithTitle: @"원" atIndex: 3 animated: NO ];        
    [self.accountSegment2 addTarget:self action:@selector(accountSegmentAction:) forControlEvents:UIControlEventValueChanged];
    self.accountSegment2.tag = kViewTag;


    
    self.shopTextField = [self createTextFieldWithFrame:CGRectMake(kTextFieldLeftMargin, 8.0, kTextFieldWidth/2, kTextFieldHeight)];
    self.shopTextField.tag = kViewTag;
    self.shopSearchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.shopSearchButton.frame = CGRectMake(kTextFieldWidth/2+30, 8.0, kTextFieldWidth/2-10, kTextFieldHeight);
    [self.shopSearchButton setTitle:@"거래처목록" forState:UIControlStateNormal];
    [self.shopSearchButton addTarget:self action:@selector(shopBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.shopSearchButton.tag = kViewTag;
    
    self.payLabel = [[UILabel alloc] initWithFrame:basicFrame];
    [self.payLabel setText:[_writeDic objectForKey:@"payTitle"]];
    self.payLabel.tag = kViewTag;
    self.payTypeSegment = [[UISegmentedControl alloc] initWithFrame:secondFrame];
    [self.payTypeSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [self.payTypeSegment setMomentary:YES];        
    [ self.payTypeSegment insertSegmentWithTitle: @"현금" atIndex: 0 animated: NO ];
    [ self.payTypeSegment insertSegmentWithTitle: @"카드" atIndex: 1 animated: NO ];
    [ self.payTypeSegment insertSegmentWithTitle: @"은행" atIndex: 2 animated: NO ];
    self.payTypeSegment.selectedSegmentIndex = 0;
    [self.payTypeSegment addTarget:self action:@selector(paySegmentAction:) forControlEvents:UIControlEventValueChanged];
    self.payTypeSegment.tag = kViewTag;
    
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
	df.dateStyle = NSDateFormatterLongStyle;
    self.writeDayButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.writeDayButton.frame = basicFrame;
    [self.writeDayButton setTitle:[NSString stringWithFormat:@"%@", [df stringFromDate:[NSDate date]]] forState:UIControlStateNormal];
    [self.writeDayButton addTarget:self action:@selector(writeDayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.writeDayButton.tag = kViewTag;
    [df release];

    self.repeatSegment = [[UISegmentedControl alloc] initWithFrame:basicFrame];
    [self.repeatSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    [ self.repeatSegment insertSegmentWithTitle: @"안함" atIndex: 0 animated: NO ];
    [ self.repeatSegment insertSegmentWithTitle: @"매주" atIndex: 1 animated: NO ];
    [ self.repeatSegment insertSegmentWithTitle: @"매월" atIndex: 2 animated: NO ];
    [ self.repeatSegment insertSegmentWithTitle: @"매년" atIndex: 3 animated: NO ];    
    self.repeatSegment.selectedSegmentIndex = 0;
    [self.repeatSegment addTarget:self action:@selector(repeatSegmentAction:) forControlEvents:UIControlEventValueChanged];
    self.repeatSegment.tag = kViewTag;

    self.tagTextField = [self createTextFieldWithFrame:CGRectMake(kTextFieldLeftMargin, 8.0, kTextFieldWidth/2, kTextFieldHeight)];
    self.tagTextField.tag = kViewTag;
    self.tagSearchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.tagSearchButton.frame = CGRectMake(kTextFieldWidth/2+30, 8.0, kTextFieldWidth/2-10, kTextFieldHeight);
    [self.tagSearchButton setTitle:@"태그목록" forState:UIControlStateNormal];
    [self.tagSearchButton addTarget:self action:@selector(tagBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.tagSearchButton.tag = kViewTag;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) loadDefaultData{
    SQLHandler* sql = [SQLHandler sharedInstance];
    NSDictionary *defCashDic = [sql findDefaultCash];
    [_writeDic setObject:[NSNumber numberWithInt:PAY_CASH] forKey:@"payType"];
    [_writeDic setObject:[defCashDic objectForKey:@"cash_id"] forKey:@"payId"];
    [_writeDic setObject:[NSString stringWithFormat:@"현금 > %@",[defCashDic objectForKey:@"title"]] forKey:@"payTitle"];
}
#pragma mark -
#pragma mark UISegmentedDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)accountSegmentAction:(id)sender{
    NSLog(@"accountSegmentAction");
    
    NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
    [currencyFormatter setGroupingSize:3];
    [currencyFormatter setGroupingSeparator:@","];
    [currencyFormatter setUsesGroupingSeparator:YES];

    unsigned long long  addAccount = 0;
    if(sender==_accountSegment){
        switch ([_accountSegment selectedSegmentIndex]) {
            case 0:
                addAccount = 10000000;
                break;
            case 1:
                addAccount = 1000000;
                break;
            case 2:
                addAccount = 100000;
                break;
            case 3:
                addAccount = 10000;
                break;
        }
    }else if(sender==_accountSegment2){
        switch ([_accountSegment2 selectedSegmentIndex]) {
            case 0:
                addAccount = 1000;
                break;
            case 1:
                addAccount = 100;
                break;
            case 2:
                addAccount = 10;
                break;
            case 3:
                addAccount = 1;
                break;
        }
    }
    unsigned long long totalAccount = [[[_accountTextField text] stringByReplacingOccurrencesOfString:@"," withString:@""] longLongValue];
    totalAccount += addAccount;
//    NSLog(@"account %llu, %llu", totalAccount, addAccount);
    [_accountTextField setText:[currencyFormatter stringFromNumber:[NSNumber numberWithLongLong:totalAccount]]];
    [currencyFormatter release];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)paySegmentAction:(id)sender{
    NSLog(@"paySegmentAction");
    switch ([_payTypeSegment selectedSegmentIndex]) {
        case PAY_CASH:
            _payPickerViewController.payType = PAY_CASH;
            break;
        case PAY_CARD:
            _payPickerViewController.payType = PAY_CARD;
            break;
        case PAY_BANK:
            _payPickerViewController.payType = PAY_BANK;
            break;
    }

    [[self navigationController] pushViewController:_payPickerViewController animated:YES];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)repeatSegmentAction:(id)sender{
    NSLog(@"repeatSegmentAction");
}
#pragma mark -
#pragma mark PickerResultDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)applyResultSet:(id)sender resultDic:(NSDictionary *)resultDic{
    NSLog(@"result:%@", resultDic);
    if([sender isKindOfClass:[PayPickerViewController class]]){
        if(resultDic==nil){
            [_writeDic removeObjectForKey:@"payId"];
            [_payLabel setText:@""];    
        }else{
            [_writeDic setObject:[resultDic objectForKey:@"payId"] forKey:@"payId"];
            [_payLabel setText:[resultDic objectForKey:@"showName"]];                
        }
    }else if([sender isKindOfClass:[ItemPickerViewController class]]){
        [_writeDic setObject:[resultDic objectForKey:@"itemGroupId"] forKey:@"itemGroupId"];
        [_writeDic setObject:[resultDic objectForKey:@"itemId"] forKey:@"itemId"];        
        [_itemBtn setTitle:[resultDic objectForKey:@"showName"] forState:UIControlStateNormal];
    }else if([sender isKindOfClass:[ShopPickerViewController class]]){
        if(resultDic==nil){
            [_writeDic removeObjectForKey:@"shopId"];        
            [_writeDic removeObjectForKey:@"shopName"];
            [_shopTextField setText:@""];
        }else{
            [_writeDic setObject:[resultDic objectForKey:@"shopId"] forKey:@"shopId"];        
            [_writeDic setObject:[resultDic objectForKey:@"shopName"] forKey:@"shopName"];
            [_shopTextField setText:[resultDic objectForKey:@"shopName"]];            
        }
    }else if([sender isKindOfClass:[DayPickerViewController class]]){
        [_writeDic setObject:[resultDic objectForKey:@"writeDay"] forKey:@"writeDay"];        
        [_writeDayButton setTitle:[resultDic objectForKey:@"showName"] forState:UIControlStateNormal];
    }else if([sender isKindOfClass:[TagPickerViewController class]]){
        if(resultDic==nil){
            [_writeDic removeObjectForKey:@"tagId"];        
            [_writeDic removeObjectForKey:@"tagName"];
            [_tagTextField setText:@""];
        }else{
            [_writeDic setObject:[resultDic objectForKey:@"tagId"] forKey:@"tagId"];        
            [_writeDic setObject:[resultDic objectForKey:@"tagName"] forKey:@"tagName"];        
            [_tagTextField setText:[resultDic objectForKey:@"tagName"]];            
        }
    }
}
#pragma mark -
#pragma mark UITextFieldDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
	[textField resignFirstResponder];
	return YES;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)accountTextEditingDidBegin{
    NSLog(@"accountTextEditingDidBegin");
}
#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)submitAction{
    NSLog(@"submitBtnAction");
    if([[_titleTextField text] length]==0){
        [CommonUtil alert:@""];
        return;
    }
    
    NSCharacterSet *alphaNums = [NSCharacterSet decimalDigitCharacterSet];
    NSCharacterSet *inStringSet = [NSCharacterSet characterSetWithCharactersInString:_accountTextField.text];
    if ([[_accountTextField text] length]==0 || ![alphaNums isSupersetOfSet:inStringSet]){
        [CommonUtil alert:@""];
        return;        
    }
}


#pragma mark -
#pragma mark UIButtonDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)itemBtnAction:(id)sender{
//    NSLog(@"itemBtnAction");
    [[self navigationController] pushViewController:_itemPickerViewController animated:YES];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)shopBtnAction:(id)sender{
//    NSLog(@"shpoBtnAction");
    [[self navigationController] pushViewController:_shopPickerViewController animated:YES];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)writeDayBtnAction:(id)sender{
//    NSLog(@"writeDayBtnAction");
    [[self navigationController] pushViewController:_dayPickerViewController animated:YES];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tagBtnAction:(id)sender{
//    NSLog(@"tagBtnAction");
    [[self navigationController] pushViewController:_tagPickerViewController animated:YES];
}

#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"didSelectRowAtIndexPath");
}

#pragma mark -
#pragma mark UITableViewDataSource
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    NSLog(@"numberOfSectionsInTableView");
	return [self.dataSourceArray count];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    NSLog(@"titleForHeaderInSection");
	return [[self.dataSourceArray objectAtIndex: section] valueForKey:kSectionTitleKey];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"numberOfRowsInSection");
	return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSNumber* cellType = (NSNumber*)[[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kSourceKey];
//    NSLog(@"heightForRowAtIndexPath cennType:%d", [cellType intValue]);
    switch ([cellType intValue]) {
        case CELL_TITLE:
            return 42;
            break;
        case CELL_ITEM:
            return 42;
            break;
        case CELL_ACCOUNT:
            return 120;
            break;
        case CELL_SHOP:
            return 42;
            break;
        case CELL_PAY:
            return 78;
            break;
        case CELL_WRITE_DAY:
            return 42;
            break;
        case CELL_REPEAT:
            return 42;
            break;
        case CELL_TAG:
            return 42;
            break;
        default:
            return 42;
            break;
    }
    
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"cellForRowAtIndexPath");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellTextField_ID];
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:kCellTextField_ID] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else{
        for(UIView *view in [cell subviews]){
            if(view.tag == kViewTag){
                [view removeFromSuperview];
            }
        }
    }

    NSNumber* cellType = (NSNumber*)[[self.dataSourceArray objectAtIndex: indexPath.section] valueForKey:kSourceKey];
//    NSLog(@"cellForRowAtIndexPath cennType:%d", [cellType intValue]);
    switch ([cellType intValue]) {
        case CELL_TITLE:
            [cell addSubview:_titleTextField];
            break;
        case CELL_ITEM:
            [cell addSubview:_itemBtn];
            break;
        case CELL_ACCOUNT:
            [cell addSubview:_accountTextField];
            [cell addSubview:_accountSegment];
            [cell addSubview:_accountSegment2];
            break;
        case CELL_SHOP:
            [cell addSubview:_shopTextField];
            [cell addSubview:_shopSearchButton];
            break;
        case CELL_PAY:
            [cell addSubview:_payLabel];
            [cell addSubview:_payTypeSegment];
            break;
        case CELL_WRITE_DAY:
            [cell addSubview:_writeDayButton];
            break;
        case CELL_REPEAT:
            [cell addSubview:_repeatSegment];
            break;
        case CELL_TAG:
            [cell addSubview:_tagTextField];
            [cell addSubview:_tagSearchButton];
            break;
        default:
            break;
    }
	
    return cell;
}



@end
